﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using LispInterpretor.HelperClasses;

namespace LispInterpretor.InBuiltProcedures.ListOperations
{
    public class Concat : Procedure
    {
        public override object Apply(params object[] args)
        {
            Check.IsTrue(args.Length > 1, "Concat needs at least two arguments.");

            var result = new Collection<object>();

            foreach (object item in args)
            {
                if (item is IList<object>)
                {
                    AppendList((IList<object>) item, result);
                }
                else 
                {
                    result.Add(item);
                }
            }

            return result;
        }

        private void AppendList(IList<object> source, IList<object> target)
        {
            foreach (object item in source)
            {
                target.Add(item);
            }
        }
    }
}
